var optRow = null;
var config = {
    table:{
        title:'',
        rownumbers: true,
        pagination: false,
        onDblClickCell: component.extend.onClickCell,
        onBeforeEdit:function(index,row){
            component.extend.add();
        },
        columns:[
            [
                {field:'name',title:'名称', align:'center'},
                {field:'val',title:'数值', align:'left', width:100,
                    formatter:function (val, row, index) {
                        try{
                            if(row.type == 9){
                                var options = row.options;
                                var txt = '';
                                $.each(options, function (index, obj) {
                                    if(val == obj.val){
                                        txt = obj.txt;
                                    }
                                })
                                return txt;
                            }else{
                                return val;
                            }
                        }catch (ex){
                            return val;
                        }
                    }
                },
                {field:'status',title:'&nbsp;', align:'center', width:50,
                    formatter:function (val, row, index) {
                        if(val == 1){
                            return '<span class="icon icon16 icon-accept">&nbsp;</span>'
                        }else if(val == 0){
                            return '<span class="icon icon16 icon-exclamation">&nbsp;</span>'
                        }else{
                            return '';
                        }
                    }
                },
            ]
        ]
    }
}
$(function () {
    action.get();
})

var tabCode = [];
var action = {
    get: function () {
        var param = {
            url: constant.route.config
        };
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            $('#opt-form-config-tab').tabs({
                onSelect:function (title,index) {
                    editTable = tabCode[index];
                }
            })
            $.each(data, function (index, obj) {
                var hasTab = $('#opt-form-config-tab').tabs('exists', obj.name);
                if(!hasTab){
                    var table = '#table-' + obj.code;
                    $('#opt-form-config-tab').tabs('add',{
                        title: obj.name,
                        content: '<table id="table-'+obj.code+'" class="data-table"></table></div>'
                    });
                    tabCode.push(table);
                    // 初始化table
                    config.table.data = obj.children;
                    component.datagrid.init(table, config.table);
                }
            })
            $('#opt-form-config-tab').tabs('select', 0);
            component.extend.init();
        })
    }
}

var column = {
    submit: function (_obj) {
        var data = {
            labelId: _obj.id,
            val: _obj.val
        }
        var param = {
            url: constant.route.config,
            data: data
        };
        return  http.post(param);
    }
}

